package com.nw.news;

import com.nw.news.common.XUtilsImageLoader;
import com.yuan.base.framework.BaseAppContext;
import com.yuan.base.framework.BaseCommonInterface;
import com.yuan.base.framework.BaseTokenManager;
import com.yuan.base.framework.BaseUserManager;
import com.yuan.base.utils.AppUtil;
import com.yuan.base.utils.UIUtil;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.File;
import java.util.HashMap;

import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ThemeConfig;


public class AppContext extends BaseAppContext implements BaseCommonInterface.HttpCallBackHandle, BaseCommonInterface.DBUtilsInterface {

	public static AppContext getInstance() {
		return (AppContext) BaseAppContext.getInstance();
	}

	@Override
	public void onCreate() {
		super.onCreate();
		// 初始化地址
		Urls.urlInit(BuildConfig.serverAddress);
//		Urls.urlInit("http://192.168.43.202");
		AppUtil.isDebug = BuildConfig.DEBUG;
		// xUtils初始化
		x.Ext.setDebug(BuildConfig.DEBUG);
		// 注册回调
		BaseCommonInterface.registerHttpCallBackHandleImpl(this);
		BaseCommonInterface.registerDbUtilsInterfaceImpl(this);
		// 初始化拍照、选图片工具
		initGalleryFinal();
	}

	public void logout() {
		BaseTokenManager.setToken(null);
		clearDbCache();
	}

	private HashMap<String, DbManager> dbCache = new HashMap<>();

	public void clearDbCache() {
		dbCache.clear();
	}

	@Override
	public DbManager getDbUtil() {
		String dbName = BaseUserManager.getUniqueCode(Constant.dbConfig.DB_NAME);
		if (dbCache.get(dbName) != null && dbCache.containsKey(dbName)) {
			return dbCache.get(dbName);
		} else {
			DbManager result = x.getDb(getDBConfig(dbName));
			dbCache.put(dbName, result);
			return result;
		}
	}

	// 数据库缓存配置
	public static DbManager.DaoConfig getDBConfig(String dbName) {
		return new DbManager.DaoConfig()
				.setDbName(dbName)
				.setDbVersion(Constant.dbConfig.DB_VERSION)
				.setAllowTransaction(true)
				.setDbUpgradeListener(new DbManager.DbUpgradeListener() {
					@Override
					public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
						// TODO: ...
						// db.addColumn(...);
						try {
							db.dropDb();
						} catch (DbException e) {
							e.printStackTrace();
						}
					}
				});
	}

	private void initGalleryFinal() {
		//设置主题
		//ThemeConfig.CYAN
		ThemeConfig theme = new ThemeConfig.Builder()
//				.setTitleBarBgColor(getResources().getColor(R.color.common_title_bg))
//				.setTitleBarTextColor(getResources().getColor(R.color.text_333))
//				.setTitleBarIconColor(getResources().getColor(R.color.text_666))
//				.setFabNornalColor(getResources().getColor(R.color.red))
//				.setFabPressedColor(getResources().getColor(R.color.orange_red))
//				.setCropControlColor(getResources().getColor(R.color.red))
//				.setIconBack(R.drawable.p_title_back_icon)
				.build();
		//配置imageloader
		CoreConfig coreConfig = new CoreConfig.Builder(getApplicationContext(), new XUtilsImageLoader(), theme)
//				.setFunctionConfig(functionConfig)
				.setEditPhotoCacheFolder(new File(getSavePath(Constant.FilePath.TEMPORARY_PATH)))//配置编辑（裁剪和旋转）功能产生的cache文件保存目录，不做配置的话默认保存在/sdcard/GalleryFinal/edittemp/
				.setTakePhotoFolder(new File(getSavePath(Constant.FilePath.TEMPORARY_PATH)))//设置拍照保存目录，默认是 / sdcard / DICM / GalleryFinal /
				.build();
		GalleryFinal.init(coreConfig);
	}

	@Override
	public void onTimeOut() {

	}

	@Override
	public void onDefaultFailure(int code, String msg) {
		UIUtil.showToast(msg);
	}


}
